package com.hx.order.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.order.mapper.SubscribeMapper;
import com.hx.order.pojo.Subscribe;
import com.hx.order.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SubscribeServiceImpl implements SubscribeService {

    @Autowired
    private SubscribeMapper subscribeMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Subscribe> findAll() {
        return subscribeMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Subscribe> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Subscribe> subscribes = (Page<Subscribe>) subscribeMapper.selectAll();
        return new PageResult<Subscribe>(subscribes.getTotal(),subscribes.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Subscribe> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return subscribeMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Subscribe> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Subscribe> subscribes = (Page<Subscribe>) subscribeMapper.selectByExample(example);
        return new PageResult<Subscribe>(subscribes.getTotal(),subscribes.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Subscribe findById(Long id) {
        return subscribeMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param subscribe
     */
    public void add(Subscribe subscribe) {
        subscribeMapper.insert(subscribe);
    }

    /**
     * 修改
     * @param subscribe
     */
    public void update(Subscribe subscribe) {
        subscribeMapper.updateByPrimaryKeySelective(subscribe);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        subscribeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Subscribe findByDoctorId(Long doctorId, Date dutyTime) {
        Subscribe subscribe=new Subscribe();
        subscribe.setDocterId(doctorId);
        subscribe.setDutyTime(dutyTime);
        Subscribe one = subscribeMapper.selectOne(subscribe);
        return one;
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Subscribe.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // docter_name
            if(searchMap.get("docterName")!=null && !"".equals(searchMap.get("docterName"))){
                criteria.andLike("docterName","%"+searchMap.get("docterName")+"%");
            }

            // num
            if(searchMap.get("num")!=null ){
                criteria.andEqualTo("num",searchMap.get("num"));
            }
            // price
            if(searchMap.get("price")!=null ){
                criteria.andEqualTo("price",searchMap.get("price"));
            }
            // mora
            if(searchMap.get("mora")!=null ){
                criteria.andEqualTo("mora",searchMap.get("mora"));
            }

        }
        return example;
    }

}
